/*
 *      CodeLife.cpp
 *      
 *      Copyright 2008 William Hamrick <brick@brick-laptop>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include <stdio.h>
#include <iostream>
#include <allegro.h>
#include <vector>
#include <math.h>
#include <time.h>
#include "Game.cpp"
#include "Graphics.cpp"
#include "AI.cpp"
#include "Network.cpp"
#include "Menu.cpp"

terrain map1;
mapobject obj;
float mousex, mousey;

void check_key(void);



void handlemouse(int flags)
{
	int i;
	if (flags & MOUSE_FLAG_MOVE)
	{
		mousey = mouse_y - map1.camera[0];
		mousex = mouse_x - map1.camera[1];
		mousey = floor(mousey / map1.size);
		mousex = floor(mousex / map1.size);
	}
	i = mousey * map1.size + mousex;
	if (flags & MOUSE_FLAG_LEFT_UP)
	{
		cout << mousex << "   " << mousey << "\n";
	}
}


int main(void)
{
	bool end_game;
	clock_t loop_time = clock();
	vector<mapobject> allmapobjects;
	allmapobjects.assign(3, obj);
	allegro_init();
	install_keyboard();
	install_mouse();
	mouse_callback = handlemouse;
	enable_hardware_cursor();
	show_mouse(NULL);
	set_gfx_mode (GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
	set_color_depth(32);
	end_game = false;
	map1 = makemap1(map1);
	allmapobjects.at(0) = startplayer(allmapobjects.at(0));
	
	for (unsigned int i = 1 ; i < allmapobjects.size() ; i++)
	{
		allmapobjects.at(i).location[0] = 64 * i;
		allmapobjects.at(i).location[1] = 64 * i;
		allmapobjects.at(i).size[0] = 32;
		allmapobjects.at(i).size[1] = 32;
		allmapobjects.at(i).pic = create_bitmap(allmapobjects.at(i).size[0], allmapobjects.at(i).size[1]);
		clear_to_color(allmapobjects.at(i).pic, makecol(255,0,0));
	}
	
	do {
		if (key[KEY_ENTER]) 
		{
			end_game = true;
		}
		if (key[KEY_LEFT])
		{
			allmapobjects.at(0).location[0]++;
		}
		if (key[KEY_RIGHT])
		{
			allmapobjects.at(0).location[0]--;
		}
		if (key[KEY_UP])
		{
			allmapobjects.at(0).location[1]++;
		}
		if (key[KEY_DOWN])
		{
			allmapobjects.at(0).location[1]--;
		}
		allmapobjects.at(0) = cameraoob(map1, allmapobjects.at(0));
		//cout << allmapobjects.at(0).location[0] << ", " << allmapobjects.at(0).location[1] << "\n";
		//cout << clock() << " " << loop_time + 1 << "\n";
		if (clock() > loop_time - 1)
		{
			clear_to_color(map1.buffer, makecol(255, 255, 255));
			blit(
				map1.map, 
				map1.buffer, 
				0, 
				0, 
				allmapobjects.at(0).location[0] + (map1.width - allmapobjects.at(0).size[0]) / 2, 
				allmapobjects.at(0).location[1] + (map1.height - allmapobjects.at(0).size[1]) / 2, 
				map1.tilewidth * map1.size, 
				map1.tileheight * map1.size
			);
			map1 = drawmapobjects(map1, allmapobjects);
			draw_sprite(map1.buffer, mouse_sprite, mouse_x, mouse_y);
			blit(map1.buffer, screen, 0, 0, 0, 0, map1.width, map1.height);
			loop_time = clock();
		}
	} while (end_game == false);
		
	return 0;
}
END_OF_MAIN();

void check_key(void)
{
	if (key[KEY_ENTER])
	{
		
	}
};
